﻿using CTP.CRM.Core.Common.SharedModel.Pub;
using Volo.Abp;

namespace CTP.CRM.Core.CurrentUs
{
    //
    // 摘要:
    //     当前请求用户
    public sealed class CurrentUserRequest
    {
        //
        // 摘要:
        //     current login user
        public static Dtos.CurrentUser CurrentUser
        {
            get
            {
                try
                {
                    IServiceProvider serviceProvider = CTPABPCore.HttpContext?.RequestServices;
                    if (serviceProvider == null)
                    {
                        return ReqHeaderResolveHelper.CreateDefaultSystemUser();
                    }

                    return serviceProvider?.GetService<Dtos.CurrentUser>() ?? ReqHeaderResolveHelper.GetRequestUserInfo();
                }
                catch (Exception)
                {
                    throw new BusinessException("401", "获取用户信息失败，请重新登录!");
                }
            }
        }

        //
        // 摘要:
        //     get the user Business id
        public static int BusinessId => ReqHeaderResolveHelper.ExtractBusinessIdOrSubSystemId("BusinessId");

        //
        // 摘要:
        //     get the user SubSystemId
        public static int SubSystemId => ReqHeaderResolveHelper.ExtractBusinessIdOrSubSystemId("SubSystemId");
    }
}
